<template>
  <div class="college-manage-container">
    <a-card class="layout-card">
      <SearchGroup
        :label-option="labelOption"
        @handleSearch="loadData(1)"
        @handleReset="reset"
        :searchOption="filterOption"
      />

      <a-table
        class="tablstyle"
        rowKey="id"
        :scroll="{ x: 1450 }"
        bordered
        :loading="loading"
        :pagination="false"
        :dataSource="list"
        :columns="columns"
      >
        <template #bodyCell="{ column, record, index, text }">
          <template v-if="column.dataIndex == 'index'">
            {{ (curPage - 1) * pageSize + (index + 1) }}
          </template>
          <template v-if="['smyc', 'sjsfyc', 'sfSm'].includes(column.dataIndex)">
            {{ text === false ? '否' : '是' }}
          </template>
          <template v-if="column.dataIndex == 'operation'">
            <a-button
              type="link"
              v-if="route.meta?.buttonList?.includes('ExaminationPaperSee')"
              @click="handleAllot(record)"
            >
              查看试卷
            </a-button>
            <!-- <a-button type="link" danger @click="handleAllot(record)"> 标记考生违纪 </a-button> -->
          </template>
        </template>
      </a-table>
      <a-pagination
        class="pagination"
        :show-total="total => `共 ${total} 条`"
        :show-size-changer="false"
        show-quick-jumper
        v-model:current="curPage"
        v-model:pageSize="pageSize"
        :total="total"
      />
    </a-card>
  </div>
</template>
<script lang="ts" setup name="ExaminationPaper">
import { onMounted, computed, watch, onActivated } from 'vue'

import { getPaperList } from '/@/api/organization/examinationPaper'
import { getKmSimpAlleList } from '/@/api/organization/subjectManage'
import { getCollegeList } from '/@/api/organization/collegeManage'

import { useList } from '/@/hooks/web/useList'
import { useRoute, useRouter } from 'vue-router'
const route = useRoute()
const router = useRouter()

import { useUserStoreWithOut } from '/@/store/modules/user'
const userStore = useUserStoreWithOut()
const addSubjectForm = computed(() => userStore.getSysConfig)

import { setAuthCache } from '/@/utils/auth'

import { columns, labelOption } from './data'

const {
  reset,
  loadData,
  filterOption,
  curPage,
  total,
  pageSize,
  list,
  loading,
  defaultFilterOption,
}: any = useList(getPaperList, {
  immediate: false,
  defaultFilterOption: {
    sfsm: 1,
  } as any,
})

if (route.query.kmdm) {
  defaultFilterOption.kmdm = 848
  labelOption.value[0].disabled = true
  reset()
}

onActivated(() => {
  loadData()
})

watch(
  () => addSubjectForm.value.showKsxxInTables,
  val => {
    labelOption.value[1].disabled = !val
  }
)

const handleAllot = row => {
  setAuthCache('examPaperInfo', row)
  router.push({
    name: 'Examinationestpaper',
    query: {
      kdkskmId: row.id,
    },
  })
}

async function getKmpyJd() {
  const { code, data } = await getKmSimpAlleList({ pageSize: 0 })
  if (code == 200) {
    data.forEach(item => (item.kmmc = `${item.kmdm}(${item.kmmc})`))
    labelOption.value[0].selectOption = data.sort((a, b) => a.kmdm - b.kmdm)
  }
}

const getCollegeOption = async () => {
  const { data, code } = await getCollegeList({
    pageSize: 0,
  })
  if (code == 200) {
    data.forEach(item => (item.xymc = `${item.xydm}(${item.xymc})`))
    labelOption.value[2]?.key == 'xydm' && (labelOption.value[2].selectOption = data.sort((a, b) => a.xydm - b.xydm))
  }
}

onMounted(() => {
  getKmpyJd()
  getCollegeOption()
})
</script>
<style lang="less" scoped>
.layout-card {
  border-radius: 8px;
}
.tablstyle {
  padding: 15px 0;
}

.btn {
  margin-left: 20px;
}
</style>
<style lang="less">
.teacher-info {
  .ant-pagination {
    margin-top: 20px;
  }
}
</style>
